package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.serach.query.SearchQueryIndex;
import cn.wolfcode.luowowo.serach.service.*;
import cn.wolfcode.luowowo.serach.template.DestinationTemplate;
import cn.wolfcode.luowowo.serach.template.StrategyTemplate;
import cn.wolfcode.luowowo.serach.template.TravelTemplate;
import cn.wolfcode.luowowo.serach.template.UserInfoTemplate;
import cn.wolfcode.luowowo.serach.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by admin on 2019/8/8.
 */
@Controller
public class IndexController {

    @Reference
    private IUserInfoRedisService redisService;

    @Reference
    private ITravelService travelService;

    @Reference
    private ITravelCommendService travelCommendService;

    @Reference
    private IStrategyCommendService strategyCommendService;

    @Reference
    private IDestinationSearchService destinationSearchService;

    @Reference
    private ITravelSearchService travelSearchService;

    @Reference
    private IStrategySearchService strategySearchService;

    @Reference
    private IUserInfoSearchService userInfoSearchService;

    @Reference
    private ISearchService searchService;

    @RequestMapping("")
    public String index(Model model, HttpServletRequest request) {

        String token = CookieUtil.getCookie(request);
        if (token != null) {
            UserInfo userInfo = redisService.getUserInfoToken(token);
            request.getSession().setAttribute("userInfo", userInfo);
        }

        model.addAttribute("tcs", travelCommendService.selectCommendTop5());
        model.addAttribute("scs", strategyCommendService.selectCommendTop5());

        return "/index/index";
    }


    @RequestMapping("index/travelPage")
    public String list(Model model, @ModelAttribute("qo") TravelQuery qo, UserInfo userInfo) {

        if (qo.getState() == -1) {
            // 查询公开的已发布的游记
            qo.setState(Travel.STATE_RELEASE);
            qo.setIsPublic(true);
        }
        qo.setPageSize(8);
        PageInfo list = travelService.query(qo);
        model.addAttribute("pageInfo", list);

       /* TravelStatisVO vo = travelStatisRedisService.getTravelStatisVo(id);
        model.addAttribute("vo", vo);*/

        return "index/travelPageTpl";
    }


    @RequestMapping("/q")
    public String search(Model model, @ModelAttribute("qo") SearchQueryIndex qo) {

        if (qo.getType() == null) {
            qo.setType(-1);
        }

        switch (qo.getType()) {
            case SearchQueryIndex.CONDITION_TYPE_USER:
                return getUser(model, qo);
            case SearchQueryIndex.CONDITION_TYPE_DEST:
                return getDest(model, qo);
            case SearchQueryIndex.CONDITION_TYPE_STRATEGY:
                return getStrategy(model, qo);
            case SearchQueryIndex.CONDITION_TYPE_TRAVEL:
                return getTravel(model, qo);
            default:
                return getAll(model, qo);
        }
    }


    // 查目的地
    private String getDest(Model model, SearchQueryIndex qo) {

        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());

        SearchResultVO data = new SearchResultVO();
        if (dest != null) {
            long total = 0;

            List<StrategyTemplate> strategys = strategySearchService.findByName(dest.getName());
            total += strategys.size();
            if (strategys.size() > 5) {
                strategys = strategys.subList(0, 5);
            }

            List<TravelTemplate> travels = travelSearchService.findByName(dest.getName());
            total += travels.size();
            if (travels.size() > 5) {
                travels = travels.subList(0, 5);
            }

            List<UserInfoTemplate> users = userInfoSearchService.findByName(dest.getName());
            total += users.size();
            if (users.size() > 5) {
                users = users.subList(0, 5);
            }
            data.setStrategys(strategys);
            data.setTotal(total);
            data.setTravels(travels);
            data.setUsers(users);
            model.addAttribute("data", data);
        }
        model.addAttribute("dest", dest);
        return "index/searchDest";
    }

    // 查游记
    private String getTravel(Model model, SearchQueryIndex qo) {

        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page", travels);
        return "index/searchTravel";
    }

    // 查用户
    private String getUser(Model model, SearchQueryIndex qo) {

        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");
        model.addAttribute("page", users);
        return "index/searchUser";
    }

    // 查攻略
    private String getStrategy(Model model, SearchQueryIndex qo) {
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page", strategys);
        return "index/searchStrategy";
    }

    // 查全部
    private String getAll(Model model, SearchQueryIndex qo) {
        // 游记
        Page<TravelTemplate> travels = searchService.searchWithHighlight(TravelTemplate.INDEX_NAME, TravelTemplate.TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");

        // 攻略
        Page<StrategyTemplate> strategys = searchService.searchWithHighlight(StrategyTemplate.INDEX_NAME, StrategyTemplate.TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");

        // 用户
        Page<UserInfoTemplate> users = searchService.searchWithHighlight(UserInfoTemplate.INDEX_NAME, UserInfoTemplate.TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "destName");

        // 目的地
        Page<DestinationTemplate> dests = searchService.searchWithHighlight(DestinationTemplate.INDEX_NAME, DestinationTemplate.TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info");

        // 数据封装
        SearchResultVO data = new SearchResultVO();
        data.setUsers(users.getContent());
        data.setTravels(travels.getContent());
        data.setStrategys(strategys.getContent());
        data.setDests(dests.getContent());
        data.setTotal(dests.getTotalElements() + travels.getTotalElements() + strategys.getTotalElements() + users.getTotalElements());
        model.addAttribute("data", data);
        return "index/searchAll";
    }

}
